<template>
  <div class="container">
    <Loading v-if="isLoading" />
    <div v-else>
      <bookHeader>目录 </bookHeader>
      <div class="pos">
        <div class="info" v-if="book">
          <div class="l">
            <img :src="book.coverImage" alt="" />
          </div>
          <div class="r">
            <div class="title">{{ book.title }}</div>
            <div class="author">{{ book.author }}</div>
          </div>
        </div>
        <div class="controll" v-if="catalog">
          <div>共{{ catalog.length }}章</div>
          <!-- <div class="btn" @click="mySort" ref="sort">倒序</div> -->
        </div>
      </div>
      <ul class="catalog">
        <li
          v-for="(item, index) in catalog"
          :key="item.uuid"
          @click="gotoContent(index, book.title)"
        >
          <span v-for="(t, i) in item.grade" :key="i">&nbsp;</span
          >{{ item.title }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { bookChapter } from "@/request/api";
import bookHeader from "@/components/bookHeader";
export default {
  components: {
    bookHeader,
  },
  data() {
    return {
      isLoading: true,
      book: null,
      catalog: [],
      //true：顺序，false：倒序
      sort: true,
    };
  },
  methods: {
    mySort() {
      if (this.sort) {
        this.$refs["sort"].innerText = "正序";
      } else {
        this.$refs["sort"].innerText = "倒序";
      }

      this.sort = !this.sort;
    },
    gotoContent(index, title) {
      this.$router.push(
        "/content?source_uuid=" +
          this.$route.query.source_uuid +
          "&index=" +
          index +
          "&title=" +
          title
      );
    },
  },
  activated() {
    bookChapter({
      source_uuid: this.$route.query.source_uuid,
    }).then((res) => {
      if (res.code == 0) {
        this.isLoading = false;
        this.book = res.data.book;
        this.catalog = res.data.catalog;
        // console.log(this.catalog);
      }
    });
  },
};
</script>

<style lang="scss" scoped>
.info {
  // padding-top: 50px;
  display: flex;
  .l {
    width: 60px;
    margin-right: 20px;
    box-shadow: 2px 3px 4px #eee;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .r {
    flex: 1;

    text-align: left;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .title {
      font-size: 18px;
      margin-bottom: 10px;
      color: #333;
    }
    .author {
      font-size: 14px;
      color: #888;
    }
  }
}
.controll {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  font-size: 16px;
  .btn {
    border: 1px solid #666;
    padding: 3px 5px;
    border-radius: 10px;
  }
}
.catalog {
  li {
    font-size: 12px;
    height: 40px;
    line-height: 40px;
    text-align: left;
    color: #404040;
    border-bottom: 1px dashed #666;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
}
.pos {
  position: sticky;
  top: 50px;
  background-color: #fff;
  z-index: 999;
}
</style>
